﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Nacos.V2;
using Nacos.V2.Config;

namespace MyWeb.ScheduledTasks.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class NacosConfigController : Controller
    {
        private readonly INacosConfigService _configClient;

        public NacosConfigController(INacosConfigService configClient)
        {
            _configClient = configClient;
        }


        [HttpGet]
        public async Task<string> Get([FromQuery] string key = "demo",string group = "DEFAULT_GROUP")
        {
            var res = await _configClient.GetConfig(dataId:key,group: group, 2000L);

            return string.IsNullOrWhiteSpace(res) ? "Not Found" : res;
        }

        [HttpGet]
        public async Task<bool> Set([FromQuery] string key = "demo", string group = "DEFAULT_GROUP",string value = "1111111")
        {

            var res = await _configClient.PublishConfig(dataId: key, group: group, content:value);

            return res;
        }

        [HttpGet]
        public async Task<bool> Remove([FromQuery] string key = "demo", string group = "DEFAULT_GROUP")
        {
            //await Task.Delay(1000);//延时1秒执行
            var res = await _configClient.RemoveConfig(dataId: key, group: group);
            return res;
        }
    }
}
